package com.cy6;

import java.util.Comparator;
import java.util.TreeSet;

/*
本次测试treeset集合的comparator比较器
 */
public class TreeSetDemo {
//    TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
//        @Override
//        public int compare(Student o1, Student o2) {
//            int num = o1.getAge() - o2.getAge();
//            int num2 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;
//            return num2;
//        }
//    });

    public static void main(String[] args) {
        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int num = o1.getAge() - o2.getAge();
                int num2 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;
                return num2;
            }
        });
        ts.add(new Student("diaochan", 25));
        ts.add(new Student("xishi", 28));
        ts.add(new Student("yangyuhuan", 30));
        ts.add(new Student("wangzhaojun", 26));
        ts.add(new Student("yuanxiang", 26));

        for (Student t : ts) {
            System.out.println(t);
        }

    }

}
